package Controllers;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import Entity.ActivityReport;
import Entity.DelayReport;
import Entity.Message;
import Entity.OperationReport;
import Entity.Report;
import Entity.Request;

public class ReportManager
{
	/*
	 * Singleton 
	 */
	private static  ReportManager Instance = null;
	private ReportManager(){}
	public static ReportManager getInstance()
	{
		if(Instance == null)
		{
			Instance = new ReportManager();
		}
		return Instance;
	}

	public Report getActivityReport(Date start, Date end) 
	{
		
	    Object[] DataArr = new Object[]{ start,  end};
		Message msg = new Message(13001, GlobalData.MESSAGE_ID(),DataArr);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (AnswerMessage.getDataGram() instanceof Report)
		{
			return (Report)AnswerMessage.getDataGram();
		}
		System.out.println("msg type unexpected when returning from getActivityReport");
		return null;
	
}

	
	public Report getOperationReport(Date start, Date end) 
	{
		
	    Object[] DataArr = new Object[]{ start,  end};
		Message msg = new Message(13002, GlobalData.MESSAGE_ID(),DataArr);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (AnswerMessage.getDataGram() instanceof Report)
		{
			return (Report)AnswerMessage.getDataGram();
		}
		System.out.println("msg type unexpected when returning from getDelayReport");
		return null;
}
	
	public Report getDelayReport(Date start, Date end) 
	{
		
	    Object[] DataArr = new Object[]{ start,  end};
		Message msg = new Message(13003, GlobalData.MESSAGE_ID(),DataArr);
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (AnswerMessage.getDataGram() instanceof Report)
		{
			return (Report)AnswerMessage.getDataGram();
		}
		System.out.println("msg type unexpected when returning from getDelayReport");
		return null;
}
	
	
	public List<Report> getAllActivityReports() 
	{
		
		Message msg = new Message(13004, GlobalData.MESSAGE_ID(),null);  // send void
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (AnswerMessage.getDataGram() instanceof List)
		{
			return (List<Report>)AnswerMessage.getDataGram();
		}
		System.out.println("msg type unexpected when returning from getDelayReport");
		return null;
}
	
}